package com.oktest.measure.item.work.provider

import com.cali.common.kt.gone
import com.cali.common.kt.invisible
import com.cali.common.kt.visible
import com.chad.library.adapter.base.entity.node.BaseNode
import com.chad.library.adapter.base.provider.BaseNodeProvider
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.oktest.measure.MyApp
import com.oktest.measure.R
import com.oktest.measure.entity.Measure
import kotlinx.android.synthetic.main.item_work_sub_01.view.*

/**
 * @Author hhh
 * @Date 2020/9/22
 */
class WorkSub1Provider : BaseNodeProvider() {
    // 遥测
    override val itemViewType: Int = 3
    override val layoutId: Int = R.layout.item_work_sub_01

    override fun convert(helper: BaseViewHolder, telemetry: BaseNode) {
        if(telemetry !is Measure) return
        helper.itemView.apply {
            tvDebugDesc.text = getDesc(telemetry)
            if (!MyApp.taskIsReceived() || telemetry.isSuccessful) {
                sub1BoxCheck.invisible()
                etValue.isEnabled = false
            } else {
                sub1BoxCheck.visible()
                etValue.isEnabled = true
            }

            if (telemetry.isSuccessful) {
                ivMark.visible()
                sub1BoxCheck.isChecked = false // 非选中
            } else {
                ivMark.invisible()
                sub1BoxCheck.isChecked = telemetry.isChecked
            }
            // 调测并失败
            if (telemetry.isDebugging && !telemetry.isSuccessful) {
                errorLayout.visible()
                tvErrorDesc.text = telemetry.errorMsg
            } else {
                errorLayout.gone()
                tvErrorDesc.text = ""
            }
            etValue.setText(telemetry.scv())
        }
    }

    private fun getDesc(measure: Measure): String {
        if (measure.sc.isEmpty() && measure.sv.isEmpty()) {
            return ""
        }
        val scv = if (measure.sc.isNotEmpty()) "二次电流${measure.sc}" else "二次电压${measure.sv}"
        val desc = "调试说明：$scv 角度${measure.angle}℃"
        return if (measure.rate.isEmpty()) {
            desc
        } else {
            desc + " 变比${measure.rate}"
        }
    }

}